/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.entity.WebServicesBinding;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class ChooseServicePanel extends Composite {
	private WebServicesBinding service;
	private ArrayList list;

	public WebServicesBinding getService() {
		return service;
	}

	public void setService(WebServicesBinding service) {
		this.service = service;
	}

	public ChooseServicePanel(ServiceMessages cons,
			ArrayList<WebServicesBinding> list) {
		this.list = list;

		VerticalPanel v = new VerticalPanel();
		v.setWidth("100%");
		v.setSpacing(5);

		v.add(new HTML(cons.chooseService()));

		Grid table = new Grid(list.size() + 1, 2);
		table.setWidth("100%");
		table.setBorderWidth(1);
		table.setCellPadding(1);
		table.setCellSpacing(1);
		
		ColumnFormatter cf = table.getColumnFormatter();
		cf.setWidth(0, "25%");
		cf.setWidth(1, "55%");

		CellFormatter ff = table.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");
		ff.setStyleName(0, 1, "tablecol");
		
		table.setHTML(0, 0, cons.service());
		table.setHTML(0, 1, cons.namespace());
		
		for (int i = 1, j = list.size(); i <= j; i++) {
			WebServicesBinding w = list.get(i - 1);
			RadioButton r = new RadioButton("service", w.getName());
			r.addClickListener(new ServiceClickListener(w,this));
			table.setWidget(i,0, r);
			table.setHTML(i,1, w.getNamespace());		
			
		}
		v.add(table);
		initWidget(v);
	}
	
	private static class ServiceClickListener implements ClickListener{
		private WebServicesBinding w;
		private ChooseServicePanel p;
		public ServiceClickListener(WebServicesBinding w,ChooseServicePanel p){
			this.w = w;
			this.p = p;
		}
		public void onClick(Widget sender) {
			p.setService(w);
		}
	}

}
